package live.zs.securitytokenauthorize.exception;

import com.auth0.jwt.exceptions.TokenExpiredException;
import live.zs.securitytokenauthorize.utils.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author madison
 * @description
 * @date 2021/7/16 23:43
 */
@ControllerAdvice
public class CustomExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionHandler.class);

    @ExceptionHandler(TokenExpiredException.class)
    private ResponseEntity<String> jwtVerificationException(TokenExpiredException e) {
        LOGGER.error("jwtVerificationException:", e);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ExceptionUtil.getAllExceptionMsg(e));
    }

    @ExceptionHandler(AccessException.class)
    private ResponseEntity<String> handleAccessException(AccessException e) {
        LOGGER.error("handleAccessException:", e);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    private ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
        LOGGER.error("handleIllegalArgumentException:", e);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getAllExceptionMsg(e));
    }

    @ExceptionHandler(Exception.class)
    private ResponseEntity<String> handleException(Exception e) {
        LOGGER.error("handleException:", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getAllExceptionMsg(e));
    }
}
